//
//  TT_GDAnnotationView.m
//  BaseProject
//
//  Created by 王洋 on 2020/4/9.
//  Copyright © 2020 王洋. All rights reserved.
//

#define kCalloutWidth       212
#define kCalloutHeight      60

#import "TT_GDAnnotationView.h"

@interface TT_GDAnnotationView ()


@end

@implementation TT_GDAnnotationView

- (id)initWithAnnotation:(id <MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self) {
        
        if (self.callOutView == nil) {
            self.callOutView = [[TT_GDCallOutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
            self.callOutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x + 15,
                                                  -CGRectGetHeight(self.callOutView.bounds) / 2.f + self.calloutOffset.y);
        }

        [self.callOutView setTitle:self.annotation.title];
        [self.callOutView setDesc:self.annotation.subtitle];

        self.callOutView.userInteractionEnabled = YES;
        [self.callOutView addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
           
            NSLog(@"点击了");
        }];
        
        [self addSubview:self.callOutView];
        
    }
    return self;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    if (self.selected == selected) {
//        return;
//    }
    
//    if (selected) {
//        if (self.callOutView == nil) {
//            self.callOutView = [[TT_GDCallOutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
//            self.callOutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
//                                                  -CGRectGetHeight(self.callOutView.bounds) / 2.f + self.calloutOffset.y);
//        }
//
//        [self.callOutView setTitle:self.annotation.title];
//        [self.callOutView setDesc:self.annotation.subtitle];
//
//        [self addSubview:self.callOutView];
//    } else {
//        [self.callOutView removeFromSuperview];
//    }
    [super setSelected:selected animated:animated];
}

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    
    if (view == nil) {
        CGPoint tempoint = [self.callOutView.ActionBtn convertPoint:point fromView:self];
        if (CGRectContainsPoint(self.callOutView.ActionBtn.bounds, tempoint))
        {
            view = self.callOutView.ActionBtn;
        }

 
    }
    return view;
}


@end
